package de.nak.fireDepartment.administrationAction;

import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.struts.ActionSupport;

import de.nak.fireDepartment.actionForms.CarForm;
import de.nak.fireDepartment.model.Car;
import de.nak.fireDepartment.service.CarService;
import de.nak.fireDepartment.service.LoginService;

/**
 * Action that shows the list of cars
 * 
 * @author Nils Bredow
 */
public class ShowCarListAction extends ActionSupport {

	/**
	 * This method list all cars
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return actionforward
	 * @throws Exception
	 */
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// Get the service
		WebApplicationContext ctx = getWebApplicationContext();
		CarService carService = (CarService)ctx.getBean("carService");
		
		// Validate the user-login
		LoginService loginService = (LoginService)getWebApplicationContext().getBean("loginService");
		if(!loginService.checkUserIsValidated()){
			return mapping.findForward("missinglogin");
		}
		
		// get all cars
		Collection<Car> cars = carService.listCars();
		// Store value in form
		CarForm carListForm = (CarForm) form;
		carListForm.setCars(cars);
		carListForm.setErrorCode("");
		// Return result
		return mapping.findForward("success");
	}
}
